﻿(function(){
//歌曲列表
var Song = function() {
	
	this.type = 1;
	//首次显示数据条数
	this.firstCount = 5;
	//点击更多之后显示的数据条数
	this.count = 5;
	this.content = $("#dataBody");
	// 初始化
	this.init();
}

Song.prototype = {
	init : function() {
		// 堆栈指针初始化
		this.stack = 0;
		// 图片路径
		this.imgPath = '../static/data/image';
		
		this.songJson = {};
		this.createList(true);
	},

	// 创建内容列表
	createList : function(boolen) {
		this.loadList(boolen);
		this.createMore();
	},
   
	// 创建更多按钮
	createMore : function() {
		this.moreTarget = $("#more");
		var _this = this;
		this.moreTarget.on('click',function(){_this.createList(false);});
	},
	
	// 加载列表
	loadList : function(boolen) {
		var _this = this;
		var oLength;
			if (boolen) { // 计算加载歌曲数
				$.get(url.SONG_LIST, function(data) {
					_this.songJson = data.list;
				   oLength = _this.songJson.length > _this.firstCount ? _this.firstCount
							: _this.songJson.length;
				   _this.showCount(oLength);
				});
				
			} else {
				oLength = (this.songJson.length - this.stack) > this.count ? this.count
						: (this.songJson.length - this.stack);
				this.showCount(oLength);
			}
			
	},
	
	showCount : function(oLength){
		if (oLength <= 0) {
			this.moreTarget.text("这是最后一页了！");
		}
		;

		if (!this.moreTarget[0]) {
			this.content.append(this.moreNode);
		}
		;

		for (var i = 0; i < oLength; i++) {
			this.showView(this.songJson);
		}
	},

	//把从后台拿到的json数据展现到页面
	showView : function(oList) {
		switch (this.type) {
		// 根据不同的模块 定制不同的数据展示形式
		case 1:
			this.content.append('<tr><td>'+'<a href="'+url.UPDATE_SONG_PAGE(oList[this.stack].id)+'">'+oList[this.stack].songName+'</a></td>'
					+'<td>'+oList[this.stack].singer+'</td>'
					+'<td>'+oList[this.stack].album+'</td>'
					+'<td>'+oList[this.stack].songInfo.dayAmout+'</td>'
					+'<td>'+oList[this.stack].songInfo.weekAmout+'</td>'
					+'<td>'+oList[this.stack].songInfo.monthAmout+'</td>'
					+'<td>'+oList[this.stack].songInfo.collectAmout+'</td>'
					+'<td>'+oList[this.stack].fileName+'</td>'
					+'<td>'+oList[this.stack].imageName+'</td>'
					+ '<td id="delete_btn'+oList[this.stack].id+'"><input  class="btn" type="button" value="删除"/></td></tr>'
					);// oncick="javascript:song.deleteSong('+oList[this.stack].id+');"
			this.addDeleteListener(oList[this.stack].id);
			break;
		case 2:
			this.content[0].innerHTML = '此模块建设中...';
			break;
		default:
			alert("该模块出错！");
		}
		this.stack += 1;
	},
	
	addDeleteListener : function(id){
		 $("#delete_btn"+id).undelegate();
		 var _this = this; 
		$("#delete_btn"+id).delegate("input","click",function(){
			_this.deleteSong(id);
		});
	},
	deleteSong : function(pid){
		$.get(url.DELETE_SONG(pid), function(data){
			if(data.code == "0"){
				$("#message").show();
				$("#delete_btn"+pid).parent().remove();
			}else if(data.code == "1") {
				$("#message").html(data.message).show();
			}
		});
	}
}
window.song = new Song();
})();